/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DTO;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author DatPLT
 */
public class TaskAddSAXParser extends DefaultHandler {

    private Task task = null;
    private Date startDay = null;
    private Date endDay = null;
    private boolean btaskName = false;
    private boolean bminestoneID = false;
    private boolean busername = false;
    private boolean bdescription = false;
    private boolean bstartDay = false;
    private boolean bendDay = false;
    private boolean bstatus = false;
    private boolean bsyear = false;
    private boolean bsmonth = false;
    private boolean bsday = false;
    private boolean beyear = false;
    private boolean bemonth = false;
    private boolean beday = false;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equals("task")) {
            task = new Task();
        }
        if (qName.equals("taskName")) {
            btaskName = true;
        }
        if (qName.equals("minestoneID")) {
            bminestoneID = true;
        }
        if (qName.equals("username")) {
            busername = true;
        }
        if (qName.equals("description")) {
            bdescription = true;
        }
        if (qName.equals("startDay")) {
            startDay = new Date();
            bstartDay = true;
        }
        if (qName.equals("year") && bstartDay) {
            bsyear = true;
        }
        if (qName.equals("month") && bstartDay) {
            bsmonth = true;
        }
        if (qName.equals("day") && bstartDay) {
            bsday = true;
        }
        if (qName.equals("endDay")) {
            endDay = new Date();
            bendDay = true;
        }
        if (qName.equals("year") && bendDay) {
            beyear = true;
        }
        if (qName.equals("month") && bendDay) {
            bemonth = true;
        }
        if (qName.equals("day") && bendDay) {
            beday = true;
        }
        if (qName.equals("status")) {
            bstatus = true;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) {
        String str = new String(ch, start, length);
        str = str.trim();
        if (btaskName) {
            task.setTaskName(str);
            btaskName = false;
        }
        if (bminestoneID) {
            task.setMinestoneID(Integer.parseInt(str));
            bminestoneID = false;
        }
        if (busername) {
            task.setUsername(str);
            busername = false;
        }
        if (bdescription) {
            task.setDescription(str);
            bdescription = false;
        }

        if (bsyear && bstartDay) {
            startDay.setYear(Integer.parseInt(str));
            bsyear = false;
        }
        if (bstartDay) {
            int a = 0;
        }
        if (bsmonth && bstartDay) {
            startDay.setMonth(Integer.parseInt(str));
            bsmonth = false;
        }
        if (bsday && bstartDay) {
            startDay.setDay(Integer.parseInt(str));
            bsday = false;
            bstartDay = false;
            task.setStartDay(startDay);
        }

        if (beyear && bendDay) {
            endDay.setYear(Integer.parseInt(str));
            beyear = false;
        }
        if (bemonth && bendDay) {
            endDay.setMonth(Integer.parseInt(str));
            bemonth = false;
        }
        if (beday && bendDay) {
            endDay.setDay(Integer.parseInt(str));
            beday = false;
            bendDay = false;
            task.setEndDay(endDay);
        }
        if (bstatus) {
            task.setStatus(str);
            bstatus = false;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equals("startDay")) {
            bstartDay = false;
        }
        if (qName.equals("endDay")) {
            bendDay = false;
        }
        if (qName.equals("task")) {
            try {
                this.task = task.InsertTask();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(TaskAddSAXParser.class.getName()).log(Level.SEVERE, null, ex);
            }
            task = null;
        }
    }
}
